//
//  TTICategoryVC.m
//  ContentApp
//
//  Created by Jery on 2018/7/17.
//  Copyright © 2018年 翔傲科技. All rights reserved.
//

#import "TTICategoryVC.h"
#import "TTICategoryTableCell.h"
#import "APPHeader.h"
#import "TTICategoryCollectionCell.h"

#define margin              6
#define CollectionCellWidth (UIScreenWidth*3/4 - margin*4)/3

@interface TTICategoryVC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) IBOutlet UITableView *firstCategoryTableView;
@property (strong, nonatomic) IBOutlet UICollectionView *secendCategoryView;
@property (nonatomic,strong) NSMutableArray *firstCategoryArr;
@property (nonatomic,strong) NSMutableArray *secendCategoryArr;
@property (nonatomic, assign) NSUInteger selectedCategoryIdx;

@end

@implementation TTICategoryVC

#pragma mark -- 懒加载
- (NSMutableArray *)firstCategoryArr
{
    if (!_firstCategoryArr) {
        _firstCategoryArr = [NSMutableArray array];
    }
    return _firstCategoryArr;
}

- (NSMutableArray *)secendCategoryArr
{
    if (!_secendCategoryArr) {
        _secendCategoryArr = [NSMutableArray array];
    }
    return _secendCategoryArr;
}

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    NAV_INIT(self, @"分类", nil, nil, nil, nil);
    
    [self initTableView];
    [self requestData];
    
    // Do any additional setup after loading the view.
}

- (void)initTableView
{
    self.firstCategoryTableView.dataSource = self;
    self.firstCategoryTableView.delegate = self;
    
    self.firstCategoryTableView.tableFooterView = [[UIView alloc] init];
    self.firstCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.firstCategoryTableView.backgroundColor = [UIColor colorWithHexString:@"#EEEDEF"];
    
    self.secendCategoryView.delegate = self;
    self.secendCategoryView.dataSource = self;
    self.secendCategoryView.backgroundColor = [UIColor colorWithHexString:@"#EEEDEF"];
    [self.secendCategoryView registerNib:[UINib nibWithNibName:NSStringFromClass([TTICategoryCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:@"CollectionCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- 列表联动事件
- (void)setSelectedCategoryIdx:(NSUInteger)selectedCategoryIdx {
    _selectedCategoryIdx = selectedCategoryIdx;

//    [self getSecondData];
    
    [self.secendCategoryView reloadData];
}

- (void)requestData
{
    NSArray *tempArr1 = [NSArray arrayWithObjects:@"纽崔莱",@"XS",@"雅姿",@"家居科技",@"个人护理",@"海外购",@"美好生活", nil];
    [self.firstCategoryArr addObjectsFromArray:tempArr1];
    
    NSArray *tempArr2 = [NSArray arrayWithObjects:@"纽崔莱",@"XS",@"雅姿",@"家居科技",@"个人护理",@"海外购",@"美好生活", nil];
    [self.secendCategoryArr addObjectsFromArray:tempArr2];
    
    [self.firstCategoryTableView reloadData];
    
    [self setSelectedCategoryIdx:0];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.firstCategoryArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"TTICategoryTableCell";
    
    TTICategoryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:nil options:nil] lastObject];
    }
    
    cell.catagoryLab.text = [self.firstCategoryArr objectAtIndex:indexPath.row];
    
    if (self.selectedCategoryIdx == indexPath.row) {
        cell.flagView.hidden = NO;
        cell.backgroundColor = [UIColor clearColor];
    }
    else
    {
        cell.flagView.hidden = YES;
    }
    
    return cell;
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    for (TTICategoryTableCell *cell in tableView.visibleCells) {
//        cell.flagView.hidden = YES;
//    }
    
//    TTICategoryTableCell *cell = (TTICategoryTableCell *)[tableView cellForRowAtIndexPath: indexPath];

//    cell.flagView.hidden = NO;
    self.selectedCategoryIdx = indexPath.row;
    
    [self.firstCategoryTableView reloadData];
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.secendCategoryArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTICategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    cell.titleLab.text = [self.secendCategoryArr objectAtIndex:indexPath.row];
    return cell;
}


/** 每个cell的尺寸*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(CollectionCellWidth, CollectionCellWidth - 30);//根据屏幕算出尺寸
}

#pragma mark - UICollectionViewDelegateFlowLayout
/** section的margin*/
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
      return UIEdgeInsetsMake(5, margin, 5,margin);//根据屏幕算出间距
}


// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return margin - 1;
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
